/*
 *  TitleScene.cpp
 *  SpaceTrader
 *
 *  Created by Jake on 10/6/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */

#include "StatsScene.h"
#include "GuiTextBox.h"
#include "Player.h"
#include "DBConnection.h"
#include<iostream>
#include<vector>
#include <sstream> 
#include<cstring>
#define MAX_POINTS 16

StatsScene::StatsScene(Universe *universe) : universe(universe)
{
    this->universe = universe;
}

StatsScene::~StatsScene()
{
}

StatsScene::StatsScene(const StatsScene &S) : universe(S.universe) { }

StatsScene& StatsScene::operator=(const StatsScene &S){
    // check for "self assignment" and do nothing in that case
    if (this == &S) return *this;
    else {
        delete universe;
        universe = S.universe;
        return *this;
    }
}


/*
 * TODO:This function requires a bit of updating to our current
 * method of using vectors to store SDL_Surfaces
 */
state_t StatsScene::draw(SDL_Surface *screen, Player *player)
{
	enum difficulty {EASY,MEDIUM, HARD} diff;
	diff = MEDIUM;
	int points = MAX_POINTS;
	const int NUM_ARROWS = 4;
	TTF_Font *buttonFont = TTF_OpenFont( "Futura.ttc", 20 );
	SDL_Color textColor = {255,255,255};
	
	SDL_Surface *enterName1 = TTF_RenderText_Blended( buttonFont, "Username:", textColor  );
	SDL_Surface *enterName2 = TTF_RenderText_Blended( buttonFont, "Password:", textColor  );

	SDL_Surface *startGame_text = TTF_RenderText_Blended( buttonFont, "Start Game", textColor  );
	SDL_Surface *easy_text = TTF_RenderText_Blended( buttonFont, "Easy", textColor  );
	SDL_Surface *medium_text = TTF_RenderText_Blended( buttonFont, "Normal", textColor  );
	SDL_Surface *hard_text = TTF_RenderText_Blended( buttonFont, "Hard", textColor  );
	SDL_Surface *backButton_text = TTF_RenderText_Blended( buttonFont, "Main Menu", textColor  );

	//moving background
	SDL_Surface *background1 = load_image("starfield.png",false);
	SDL_Surface *background2 = load_image("starfield2.png",false);
	SDL_Surface *check = load_image("check.png",false);
	SDL_Surface *xmark = load_image("xmark.png",false);


	int scrollSpeed = 2;
	int x1 = 0;
	int x2 = 0;
	int count = 0;
	bool check_visible = false;
	
	GuiButton easy(80, 325);
	GuiButton medium(230, 325);
	GuiButton hard(380, 325);
	GuiButton backButton(0, 425);

	//moving background
	
	//std::vector<GuiButton*> rightArrow(NUM_ARROWS);
	//std::vector<GuiButton*> leftArrow(NUM_ARROWS);

	GuiButton *rightArrow = (GuiButton*)(::operator new(sizeof(GuiButton)*NUM_ARROWS));
	GuiButton *leftArrow = (GuiButton*)(::operator new(sizeof(GuiButton)*NUM_ARROWS));
	int stats[NUM_ARROWS];
	std::string attributes[] = {"Pilot:", "Fighter:", "Trader:", "Engineer:"};
	
	for ( int i = 0; i < NUM_ARROWS; i++) {
		new(&rightArrow[i]) GuiButton(220,60*i+40,"GuiArrowR", "");
		new(&leftArrow[i]) GuiButton(120,60*i+40,"GuiArrowL", "");
		stats[i] = 0;
	}
	

	SDL_Event event;
	state_t next_state = STATS;
	//Initialize Objects
	GuiButton startGame(490,425);
	GuiTextBox textBox1(355,175);
	GuiTextBox textBox2(355,225);


	while (next_state==STATS) {
		
		//Event Loop
		while (SDL_PollEvent(&event)) {
			if(points==0 && textBox1.getText()!="" && textBox2.getText()!="" ) {
				if (startGame.handle_input(event) == CLICKED){
					//Add user to database
					DBAddUser(textBox1.getText(), textBox2.getText());

                    next_state = PLANET;
                }
			} else {
				if (startGame.getState()!=2) startGame.setState(2);
			}
			if (backButton.handle_input(event) == CLICKED) next_state = TITLE;
			if (event.type==SDL_QUIT) next_state = QUIT;
			if (textBox1.handle_input(event, buttonFont, textColor) == CLICK) {
				textBox1.focus();
				textBox2.unFocus();
			}
			if (textBox2.handle_input(event, buttonFont, textColor) == CLICK) {
				textBox2.focus();
				textBox1.unFocus();
			}
			//toggle buttons
			switch(diff) {
				case EASY:
					easy.setState(1);
					if(medium.handle_input(event) == CLICKED) {
						diff = MEDIUM;
						easy.setState(0);
					}
					if(hard.handle_input(event) == CLICKED) {
						diff = HARD;
						easy.setState(0);
					}
					break;
				case MEDIUM:
					medium.setState(1);
					if(easy.handle_input(event) == CLICKED) {
						diff = EASY;
						medium.setState(0);
					}
					if(hard.handle_input(event) == CLICKED) {
						diff = HARD;
						medium.setState(0);
					}
					break;
				case HARD:
					hard.setState(1);
					if(easy.handle_input(event) == CLICKED) {
						diff = EASY;
						hard.setState(0);
					}
					if(medium.handle_input(event) == CLICKED) {
						diff = MEDIUM;
						hard.setState(0);
					}
					break;
			}


			
			//Arrow illumination logic
			for ( int i = 0; i < NUM_ARROWS; i++) {
				if (stats[i]>0)  {
					if(leftArrow[i].getState()==2) rightArrow[i].setState(0);

					if(leftArrow[i].handle_input(event) == CLICKED) {
						stats[i]--;
						points++;
					}
				} else {
					if(stats[i]==0) leftArrow[i].setState(2);
				}
				
				if (points>0)  {
					if(rightArrow[i].getState()==2) rightArrow[i].setState(0);
					
					if(leftArrow[i].getState()==2 && stats[i]>0) leftArrow[i].setState(0);
					
					if(rightArrow[i].handle_input(event) == CLICKED) {
						stats[i]++;
						points--;
					}
				} else {
					if(points==0) rightArrow[i].setState(2);
				}
			}

		}
		//Apply objects
		SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00));
		
		//Moving Background
		if (x1 <= (-1) * SCREEN_WIDTH) x1 = 0;
		if (x2 <= (-1) * SCREEN_WIDTH) x2 = 0;
		apply_surface( x1, 0, background1, screen );
		apply_surface( x2, 0, background2, screen );
		apply_surface( x1 + SCREEN_WIDTH, 0, background1, screen );
		apply_surface( x2 + SCREEN_WIDTH, 0, background2, screen );
		x1 -= scrollSpeed;
		x2 -= (scrollSpeed / 2);
		
		startGame.show(screen);
		textBox1.show(screen);
		textBox2.show(screen);
		apply_surface(360, 150, enterName1, screen);
		apply_surface(360, 200, enterName2, screen);

		apply_surface(515, 440, startGame_text, screen);
		std::stringstream pointsRem;
		
		pointsRem << "Points remaining: " << points;
		SDL_Surface *pointsRem_text = TTF_RenderText_Blended( buttonFont, pointsRem.str().c_str(), textColor  );
		apply_surface(20, 0, pointsRem_text, screen);
		SDL_FreeSurface(pointsRem_text);

		easy.show(screen);
		medium.show(screen);
		hard.show(screen);
		backButton.show(screen);
		apply_surface(130, 340, easy_text, screen);
		apply_surface(270, 340, medium_text, screen);
		apply_surface(430, 340, hard_text, screen);
		apply_surface(20, 440, backButton_text, screen );
		if(check_visible) apply_surface( 560, 175, check, screen );
		else apply_surface( 560, 175, xmark, screen );
		if(count == 100) {
			check_visible = DBCheckUser(textBox1.getText())?false:true;
			count = 0;

		}
		count++;

		for ( int i = 0; i < NUM_ARROWS; i++) {
			//arrows
			rightArrow[i].show(screen);
			leftArrow[i].show(screen);
			
			//attribute names
			SDL_Surface *attrib_text = TTF_RenderText_Blended( buttonFont, attributes[i].c_str(), textColor  );
			apply_surface(20, 60*i+50, attrib_text, screen);
			SDL_FreeSurface(attrib_text);
			
			//attribute points
			std::stringstream temp;
			temp << "" << stats[i];
			SDL_Surface *num_text = TTF_RenderText_Blended( buttonFont, temp.str().c_str(), textColor  );
			apply_surface(185, 60*i+50, num_text, screen);
			SDL_FreeSurface(num_text);

		}
		//Update the screen
		if ( SDL_Flip( screen ) == -1 ) {
			std::cerr << "Error Updating screen." << std::endl;
		}
	}
	player->setName(textBox1.getText());
	player->setStats(stats);
	SDL_FreeSurface(background1);
	SDL_FreeSurface(background2);
	SDL_FreeSurface(check);
	SDL_FreeSurface(xmark);

	//std::cout << "Player name: " << player->getName()<<std::endl;
	for (int i = 0; i<NUM_ARROWS;i++) {}
		//std::cout << attributes[i] << player->getStats()[i] << std::endl;
	//std::cout << "Difficulty: " << diff << std::endl;
	return next_state;
}
